<?php
/**
 * Created by PhpStorm.
 * User: harley
 * Date: 16/5/12
 * Time: 下午2:48
 */
//error_reporting(E_ALL);
//ini_set("display_errors","off");

require_once ("Harley_DBF.php");
require_once ("Utils.php");
require_once ("Global_Config.php");
require_once ("String.class.php");
require_once ("Des.class.php");
require_once ("UserAuth.php");

header('Access-Control-Allow-Origin:*');

$type = $_SERVER['REQUEST_METHOD'];
parse_str(file_get_contents('php://input'), $data);
$data = array_merge($_GET, $_POST, $data);
$data2 = $data['data'];
$arr = json_decode($data2,true);
extract($arr,true);

$UserID_value = isset($UserID)?$UserID:"";

$UserName_value = isset($UserName)?$UserName:"";

$PasswordMD5_value = isset($PasswordMD5)?$PasswordMD5:"";

$sqlArr = array('AND'=>array('UserID[=]'=>$UserID_value)) ;

// 筛选 按ASIN
if(isset($ASIN)){
    $sqlArr['AND']['ASIN[=]'] = $ASIN;
}
// 筛选 按店铺
if(isset($MarketID)){
    $sqlArr['AND']['MarketID[=]'] = $MarketID;
}
// 筛选 按时间
if(isset($ModifyPriceTime) && !empty($ModifyPriceTime)){
    $sqlArr['AND']['#ModifyPriceTime[=]'] = "to_days(date_format(from_UNIXTIME(`ModifyPriceTime`),'%Y-%m-%d')) = to_days(".$ModifyPriceTime.");";
}


//检测是否对应的用户

$user_auth = new UserAuth();

if(!$user_auth->CheckUserAuth($UserName_value, $PasswordMD5_value))
{
    $respon = array('code' => '400',
        'msg' => '授权错误，请检测对应的账号与密码字段！'
    );
    echo JSON($respon);
    exit(0);
}


$database = new Harley_DBF([
    // required
    'database_type' => 'mysql',
    'database_name' => DATABASE_NAME,
    'server' => DATABASE_SERVER,
    'username' => DATABASE_USERNAME,
    'password' => DATABASE_PASSWORD,
    'charset' => 'utf8',
    // [optional]
    'port' => 3306,
    // [optional] Table prefix
    'prefix' => "",
    // driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php
    'option' => [
        PDO::ATTR_CASE => PDO::CASE_NATURAL
    ]
]);

$tableName = "usermodifypricehistory"; // 记住不用加 opolo_ 前缀了

$datas = [];

$respon = [];

$secretkey = "Harley88888"; ///请勿随意修改此值，以避免解密出现错误

$desUtils = new DesUtils();

$finaldatas = [];

if (''===$UserID_value && ''=== $PasswordMD5_value)
{
    $respon = array('code' => '200',
        'msg' => 'Success',
        "values" => ""
    );

} else {

    $datas = $database->select($tableName, "*",$sqlArr);

    $respon = array('code' => '200',
        'msg' => 'Success',
        "values" => $datas
    );
}
echo JSON($respon);